package com.ruoyi.system.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.system.domain.PurchaseOrders;
import com.ruoyi.system.mapper.vendorMapper;
import com.ruoyi.system.domain.vendor;
import com.ruoyi.system.service.IvendorService;

/**
 * 供应商详情Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-08-14
 */
@Service
public class vendorServiceImpl implements IvendorService 
{
    @Autowired
    private vendorMapper vendorMapper;

    /**
     * 查询供应商详情
     * 
     * @param vaId 供应商详情主键
     * @return 供应商详情
     */
    @Override
    public vendor selectvendorByVaId(Long vaId)
    {
        return vendorMapper.selectvendorByVaId(vaId);
    }

    /**
     * 查询供应商详情列表
     * 
     * @param vendor 供应商详情
     * @return 供应商详情
     */
    @Override
    public List<vendor> selectvendorList(vendor vendor)
    {
        return vendorMapper.selectvendorList(vendor);
    }

    @Override
    public List<vendor> selectvendorList2(vendor vendor) {
        return vendorMapper.selectvendorList2(vendor);
    }

    /**
     * 新增供应商详情
     * 
     * @param vendor 供应商详情
     * @return 结果
     */
    @Transactional
    @Override
    public int insertvendor(vendor vendor)
    {
        int rows = vendorMapper.insertvendor(vendor);
        insertPurchaseOrders(vendor);
        return rows;
    }

    /**
     * 修改供应商详情
     * 
     * @param vendor 供应商详情
     * @return 结果
     */
    @Transactional
    @Override
    public int updatevendor(vendor vendor)
    {
        vendorMapper.deletePurchaseOrdersByVaId(vendor.getVaId());
        insertPurchaseOrders(vendor);
        return vendorMapper.updatevendor(vendor);
    }

    /**
     * 批量删除供应商详情
     * 
     * @param vaIds 需要删除的供应商详情主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deletevendorByVaIds(Long[] vaIds)
    {
        vendorMapper.deletePurchaseOrdersByVaIds(vaIds);
        return vendorMapper.deletevendorByVaIds(vaIds);
    }

    /**
     * 删除供应商详情信息
     * 
     * @param vaId 供应商详情主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deletevendorByVaId(Long vaId)
    {
        vendorMapper.deletePurchaseOrdersByVaId(vaId);
        return vendorMapper.deletevendorByVaId(vaId);
    }

    /**
     * 新增入库管理信息
     * 
     * @param vendor 供应商详情对象
     */
    public void insertPurchaseOrders(vendor vendor)
    {
        List<PurchaseOrders> purchaseOrdersList = vendor.getPurchaseOrdersList();
        Long vaId = vendor.getVaId();
        if (StringUtils.isNotNull(purchaseOrdersList))
        {
            List<PurchaseOrders> list = new ArrayList<PurchaseOrders>();
            for (PurchaseOrders purchaseOrders : purchaseOrdersList)
            {
                purchaseOrders.setVaId(vaId);
                list.add(purchaseOrders);
            }
            if (list.size() > 0)
            {
                vendorMapper.batchPurchaseOrders(list);
            }
        }
    }
}
